import api from '../../api/api'
import httpServer from '../../api/http'
import router from '../../router'
import {clearStore} from '../../utils/storage'
import {Message} from 'element-ui'

export function getRefreshToken (param) {
  let params = {
    refreshToken: param
  }
  return httpServer(api.refreshToken, params).then(
    res => {
      return Promise.resolve(res.data)
    }
  )
}

export function isRefreshTokenExpired (timestamp) {
  clearInterval(window.interval)
  // 1分钟计数减少一次
  window.interval = setInterval(() => {
    timestamp = timestamp - 1
    if (timestamp < 0) {
      router.push({ path: '/login' }).catch(err => err)
      clearInterval(window.interval)
      clearStore()
      Message({
        showClose: true,
        message: '由于您长时间未操作，系统已自动退出，请重新登录！',
        type: 'warning',
        duration: 0
      })
    }
    window.sessionStorage.setItem('expiredIn', timestamp)
  }, 60000)
}
